package com.qmx.qmxapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.qmx.qmxapp.R;
import com.qmx.qmxapp.adapter.ContactMultipleItemAdapter;
import com.qmx.qmxapp.base.BaseActivity;
import com.qmx.qmxapp.bean.ContactModel;
import com.qmx.qmxapp.data.SectionMultipleItem;
import com.qmx.qmxapp.data.Video;


import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.qmx.qmxapp.data.DataServer.HTTPS_AVATARS1_GITHUBUSERCONTENT_COM_LINK;

public class CustomerGroupActivity extends BaseActivity {
    @BindView(R.id.tip_content)
    TextView tip_content;
    @BindView(R.id.tv_group_tips)
    TextView tv_group_tips;

    private List<String> list;
    private RecyclerView recyclerView;
    private Gson mGson = new Gson();
    private List<SectionMultipleItem> modelList = new ArrayList<>();
    private String title;
    private String name;
    private String content;
    private Intent intent = new Intent();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer_group);
        ButterKnife.bind(this);

        list = new ArrayList<>();
        title = this.getIntent().getStringExtra("title");
        name = this.getIntent().getStringExtra("name");
        content = this.getIntent().getStringExtra("content");
        tv_group_tips.setText(title);
        tip_content.setText("群组");
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

        ContactMultipleItemAdapter sectionAdapter = new ContactMultipleItemAdapter(R.layout.def_section_head, getList(content));
        sectionAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                SectionMultipleItem item = (SectionMultipleItem) adapter.getData().get(position);
                switch (view.getId()) {
                    case R.id.card_view:
                        // 获取主体item相应数据给后期使用
                        String str = item.getVideo().getContent().toString();
                        if (!TextUtils.isEmpty(str)) {
                            showToast(item.getVideo().getContent().toString());
                            try {
                                Object object = new JSONTokener(str).nextValue();
                                if (object instanceof JSONObject && !hasLower(str)) {
                                    intent.setClass(CustomerGroupActivity.this, ContactDetailActivity.class);
                                    intent.putExtra("content", str);
                                    startActivity(intent);
                                } else {
                                    modelList.clear();
                                    adapter.replaceData(getList(item.getVideo().getContent().toString()));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            Log.e("onItemChildClick", item.getVideo().getContent().toString());
                            //                            startActivity(new Intent(getActivity(), CustomerGroupActivity.class));
                        }
                        break;
                    default:
                        break;
                }
            }
        });
        recyclerView.setAdapter(sectionAdapter);
    }

    private List<SectionMultipleItem> getList(String str) {
        Log.e("getDepartment", str);
        if (str != null) {
            if (name.equals("department")) {
                ContactModel.Content.Department contactModel = mGson.fromJson(str, ContactModel.Content.Department.class);
                if (contactModel.getChildlist() != null) {
                    for (ContactModel.Content.Department.Childlist childlist : contactModel.getChildlist()) {
                        Video video = new Video("department", childlist.getName(), mGson.toJson(childlist));
                        modelList.add(new SectionMultipleItem(SectionMultipleItem.TEXT, video));
                    }
                    for (ContactModel.Content.Users user : contactModel.getUsers()) {
                        Video video = new Video(HTTPS_AVATARS1_GITHUBUSERCONTENT_COM_LINK, user.getName(), mGson.toJson(user));
                        modelList.add(new SectionMultipleItem(SectionMultipleItem.IMG_TEXT, video));
                    }
                }
            }
            if (name.equals("discuss")) {
                ContactModel.Content.Discuss discuss = mGson.fromJson(str, ContactModel.Content.Discuss.class);
                if (discuss.getUsers() != null) {
                    //                    for (ContactModel.Content.Discuss.UsersX usersX : discuss.getUsers()) {
                    //                        Video video = new Video("discuss", usersX.getName(), mGson.toJson(usersX));
                    //                        modelList.add(new SectionMultipleItem(SectionMultipleItem.TEXT, video));
                    //                    }
                    for (ContactModel.Content.Discuss.UsersX user : discuss.getUsers()) {
                        Video video = new Video(HTTPS_AVATARS1_GITHUBUSERCONTENT_COM_LINK, user.getName(), mGson.toJson(user));
                        modelList.add(new SectionMultipleItem(SectionMultipleItem.IMG_TEXT, video));
                    }
                }
            }
            //            ContactModel.Content.Users users = mGson.fromJson(str, ContactModel.Content.Users.class);
            //            if (users != null) {
            //                for (ContactModel.Content.Users users : contactModel.getUsers()) {
            //
            //                    Video video = new Video(HTTPS_AVATARS1_GITHUBUSERCONTENT_COM_LINK, users.getName(), mGson.toJson(users));
            //                    modelList.add(new SectionMultipleItem(SectionMultipleItem.IMG_TEXT, video));
            //                }
            //            }
        }

        return modelList;
    }
}
